/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.ComboBox
 *  javafx.scene.control.DatePicker
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Stage
 *  javafx.util.StringConverter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.StringUtils
 */
package com.gys.controller.member;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.MemberAddOrEditUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.data.KeyValue;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.member.MemberAddOrEditController;
import com.gys.data.example.GetUserOutData;
import com.gys.data.member.GetMemberInData;
import com.gys.data.member.GetMemberOutData;
import com.gys.view.member.MemberInfoView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

@FXMLController
public class MemberInfoController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(MemberInfoController.class);
    @FXML
    private ComboBox<KeyValue> gmbSexSave;
    @FXML
    private ComboBox<KeyValue> gmbBbSexSave;
    @FXML
    private TextField gmbAddressSave;
    @FXML
    private TextField gmbNameSave;
    @FXML
    private TextField gmbCredentialsSave;
    @FXML
    private DatePicker gmbBirthSave;
    @FXML
    private TextField gmbAgeSave;
    @FXML
    private TextField gmbBbNameSave;
    @FXML
    private TextField gmbMobileSave;
    @FXML
    private TextField remarkSave;
    @FXML
    private TextField gmbTelSave;
    @FXML
    private TextField gmbBbAgeSave;
    @FXML
    private AnchorPane pane;
    @Autowired
    private MemberAddOrEditController memberAddOrEditController;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        StringConverter converter = CommonUtil.dataPickerFormat("yyyyMMdd");
        this.gmbBirthSave.setConverter(converter);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                MemberInfoView view = SpringUtil.getBean(MemberInfoView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            MemberInfoController.this.saveBtnAction();
                        } else if (e.getCode() == KeyCode.ESCAPE) {
                            MemberInfoController.this.cancelAction();
                        }
                        e.consume();
                    }
                });
            }
        }, 1000L);
        ObservableList sexOptions = FXCollections.observableArrayList(new ArrayList());
        sexOptions.add(new KeyValue("\u7537", "1"));
        sexOptions.add(new KeyValue("\u5973", "0"));
        this.gmbSexSave.setItems(sexOptions);
        this.gmbSexSave.setValue(new KeyValue());
        this.gmbBbSexSave.setItems(sexOptions);
        this.gmbBbSexSave.setValue(new KeyValue());
        this.comboxInit(this.gmbSexSave);
        this.comboxInit(this.gmbBbSexSave);
        this.initTextField(this.gmbAgeSave);
        this.initTextField(this.gmbBbAgeSave);
        if ("2".equals(this.memberAddOrEditController.cardStatus)) {
            this.initData();
        } else {
            this.clearData();
        }
    }

    private void clearData() {
        this.gmbSexSave.setValue(new KeyValue());
        this.gmbBbSexSave.setValue(new KeyValue());
        this.gmbAddressSave.clear();
        this.gmbNameSave.clear();
        this.gmbCredentialsSave.clear();
        this.gmbBirthSave.setValue(null);
        this.gmbAgeSave.clear();
        this.gmbBbNameSave.clear();
        this.gmbMobileSave.clear();
        this.remarkSave.clear();
        this.gmbTelSave.clear();
        this.gmbBbAgeSave.clear();
    }

    private void initData() {
        GetMemberInData inData = new GetMemberInData();
        inData.setClientId(((GetUserOutData)CacheUtil.get("logInUser")).getClient());
        inData.setGmbCardId(this.memberAddOrEditController.memberData.getGmbCardId());
        GetMemberOutData outData = null;
        HttpJson httpJson = MemberAddOrEditUtil.findMemberByPrimary(CommonUtil.convertToMap(inData));
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        outData = (GetMemberOutData)JSON.parseObject((String)httpJson.getMsg(), GetMemberOutData.class);
        if (ObjectUtil.isEmpty(outData)) {
            return;
        }
        this.gmbNameSave.setText(outData.getGmbName());
        this.gmbAddressSave.setText(outData.getGmbAddress());
        this.gmbMobileSave.setText(outData.getGmbMobile());
        this.gmbTelSave.setText(outData.getGmbTel());
        if ("1".equals(outData.getGmbSex())) {
            this.gmbSexSave.setValue(new KeyValue("\u7537", "1"));
        } else if ("0".equals(outData.getGmbSex())) {
            this.gmbSexSave.setValue(new KeyValue("\u5973", "0"));
        }
        this.gmbCredentialsSave.setText(outData.getGmbCredentials());
        this.gmbAgeSave.setText(outData.getGmbAge());
        String gmbBirth = outData.getGmbBirth();
        if (ObjectUtil.isNotEmpty(gmbBirth)) {
            String jFYear = gmbBirth.substring(0, 4);
            String jFMonth = gmbBirth.substring(4, 6);
            String jFDay = gmbBirth.substring(6);
            this.gmbBirthSave.setValue(LocalDate.of((int)Integer.valueOf(jFYear), Integer.valueOf(jFMonth), (int)Integer.valueOf(jFDay)));
        }
        this.gmbBbNameSave.setText(outData.getGmbBbName());
        this.remarkSave.setText(outData.getGsmbRemark());
        if ("1".equals(outData.getGmbBbSex())) {
            this.gmbBbSexSave.setValue(new KeyValue("\u7537", "1"));
        } else if ("0".equals(outData.getGmbBbSex())) {
            this.gmbBbSexSave.setValue(new KeyValue("\u5973", "0"));
        }
        this.gmbBbAgeSave.setText(outData.getGmbBbAge());
    }

    private void initTextField(TextField textField) {
        textField.textProperty().addListener((ChangeListener)new ChangeListener<String>(){

            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (ObjectUtil.isNotEmpty(newValue) && !CommonUtil.positiveInteger(newValue)) {
                    Alert alert = MemberInfoController.this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u6b63\u6574\u6570");
                    alert.showAndWait();
                    return;
                }
            }
        });
    }

    public void comboxInit(ComboBox box) {
        box.converterProperty().set(new StringConverter<KeyValue>(){

            public String toString(KeyValue object) {
                return object.getName();
            }

            public KeyValue fromString(String string) {
                return null;
            }
        });
    }

    @FXML
    private void saveBtnAction() {
        if (ObjectUtil.isEmpty(this.memberAddOrEditController.memberData)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u672a\u9009\u62e9\u5bf9\u5e94\u8bb0\u5f55\uff0c\u8bf7\u91cd\u65b0\u9009\u62e9\uff01");
            alert.showAndWait();
            return;
        }
        String gmbName = this.gmbNameSave.getText().trim();
        String gmbAddress = this.gmbAddressSave.getText().trim();
        String gmbMobile = this.gmbMobileSave.getText().trim();
        String gmbTel = this.gmbTelSave.getText().trim();
        String gmbSex = ((KeyValue)this.gmbSexSave.getValue()).getValue();
        String gmbCredentials = this.gmbCredentialsSave.getText().trim();
        String gmbAge = this.gmbAgeSave.getText().trim();
        String gmbBirth = null;
        if (ObjectUtil.isNotEmpty(this.gmbBirthSave.getValue())) {
            gmbBirth = ((LocalDate)this.gmbBirthSave.getValue()).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        }
        String gmbBbName = this.gmbBbNameSave.getText().trim();
        String gmbBbSex = ((KeyValue)this.gmbBbSexSave.getValue()).getValue();
        String gmbBbAge = this.gmbBbAgeSave.getText().trim();
        String remark = this.remarkSave.getText().trim();
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (StringUtils.isEmpty(gmbName)) {
            alert.setContentText("\u59d3\u540d\u672a\u586b\u5199\u5b8c\u6210\uff0c\u8bf7\u8f93\u5165\u540e\u4fdd\u5b58\uff01");
            alert.showAndWait();
            return;
        }
        if (gmbName.length() > 20) {
            alert.setContentText("\u59d3\u540d\u957f\u5ea6\u6700\u5927\u4e3a20");
            alert.showAndWait();
            return;
        }
        if (StringUtils.isEmpty(gmbMobile)) {
            alert.setContentText("\u624b\u673a\u672a\u586b\u5199\u5b8c\u6210\uff0c\u8bf7\u8f93\u5165\u540e\u4fdd\u5b58\uff01");
            alert.showAndWait();
            return;
        }
        if (!CommonUtil.checkPhone(gmbMobile)) {
            alert.setContentText("\u624b\u673a\u53f7\u7801\u683c\u5f0f\u9519\u8bef\uff01");
            alert.showAndWait();
            return;
        }
        if (StringUtils.isEmpty(gmbSex)) {
            alert.setContentText("\u6027\u522b\u672a\u586b\u5199\u5b8c\u6210\uff0c\u8bf7\u8f93\u5165\u540e\u4fdd\u5b58\uff01");
            alert.showAndWait();
            return;
        }
        if (StringUtils.isEmpty(gmbAge)) {
            alert.setContentText("\u5e74\u9f84\u672a\u586b\u5199\u5b8c\u6210\uff0c\u8bf7\u8f93\u5165\u540e\u4fdd\u5b58\uff01");
            alert.showAndWait();
            return;
        }
        if (ObjectUtil.isNotEmpty(gmbCredentials) && !CommonUtil.isIDNumber(gmbCredentials)) {
            alert.setContentText("\u8eab\u4efd\u8bc1\u683c\u5f0f\u9519\u8bef\uff01");
            alert.showAndWait();
            return;
        }
        if (ObjectUtil.isNotEmpty(gmbAge) && !CommonUtil.positiveInteger(gmbAge)) {
            alert.setContentText("\u5e74\u9f84\u8bf7\u586b\u5199\u6b63\u6574\u6570");
            alert.showAndWait();
            return;
        }
        if (ObjectUtil.isNotEmpty(gmbBbAge) && !CommonUtil.positiveInteger(gmbBbAge)) {
            alert.setContentText("\u5e74\u9f84\u8bf7\u586b\u5199\u6b63\u6574\u6570");
            alert.showAndWait();
            return;
        }
        GetMemberInData inData = new GetMemberInData();
        inData.setGmbCardId(this.memberAddOrEditController.memberData.getGmbCardId());
        inData.setGmbName(gmbName);
        inData.setGmbAddress(gmbAddress);
        inData.setGmbMobile(gmbMobile);
        inData.setGmbTel(gmbTel);
        inData.setGmbSex(gmbSex);
        inData.setGmbCredentials(gmbCredentials);
        inData.setGmbAge(gmbAge);
        inData.setGmbBirth(gmbBirth);
        inData.setGmbBbName(gmbBbName);
        inData.setGmbBbSex(gmbBbSex);
        inData.setGmbBbAge(gmbBbAge);
        inData.setRemark(remark);
        if ("1".equals(this.memberAddOrEditController.cardStatus)) {
            inData.setGmbStatus("2");
            HttpJson httpJson = MemberAddOrEditUtil.editMember(CommonUtil.convertToMap(inData));
            if (httpJson.isSuccess()) {
                alert.setContentText("\u4fdd\u5b58\u6210\u529f");
                this.memberAddOrEditController.initTable();
                this.cancelAction();
            } else {
                alert.setContentText(httpJson.getMsg());
            }
        } else if ("2".equals(this.memberAddOrEditController.cardStatus)) {
            HttpJson httpJson = MemberAddOrEditUtil.editMember(CommonUtil.convertToMap(inData));
            if (httpJson.isSuccess()) {
                alert.setContentText("\u66f4\u65b0\u6210\u529f");
                this.memberAddOrEditController.initTable();
                this.cancelAction();
            } else {
                alert.setContentText(httpJson.getMsg());
            }
        }
        alert.showAndWait();
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }
}

